package ph.gov.marikina.mcsmtms.services.impl;

import java.util.HashMap;
import java.util.Map;

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.SmsFactory;
import com.twilio.sdk.resource.instance.Sms;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import com.twilio.sdk.resource.list.SmsList;

import ph.gov.marikina.mcsmtms.services.SmsSenderService;

public class SmsSenderServiceImpl implements SmsSenderService {
	private static final String ACCOUNT_SID = "ACe0aaf2a8331f61d7951c67ec77975c44";
	private static final String AUTH_TOKEN = "3dc8c9bd497508b053e3cbf7578e6393";
	private static final String fromNumber = "+12242201502";
	
	/* (non-Javadoc)
	 * @see ph.gov.marikina.mcsmtms.services.impl.SmsSenderService#sendSms(java.lang.String, java.lang.String)
	 */
	@Override
	public void sendSms(String recipientNumber, String message) {
		ClientConnectionManager connectionManager = null;
        HttpClient httpClient = null;
		
	    try {
	        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
	 
	        HttpParams httpParams = new BasicHttpParams();
	        
	        Map<String, String> params = new HashMap<String, String>();
	        params.put("Body", message);
	        params.put("To", recipientNumber);
	        params.put("From", fromNumber);
	        
	        connectionManager = new GAEConnectionManager();
	        httpClient = new DefaultHttpClient(connectionManager, httpParams);
	        
	        // Build a filter for the SmsList
	        client.setHttpclient(httpClient);
	        //client.set
	        
	        SmsFactory messageFactory = client.getAccount().getSmsFactory();
	        Sms smsMessage = messageFactory.create(params);
	        
	        System.out.println(smsMessage.getSid());
	    } catch (Exception e) {
	        System.out.println(e.getMessage());
	    } finally {
	    	if(connectionManager != null)
	    		connectionManager.closeExpiredConnections();
	    		connectionManager.shutdown();
	    }
	}
	
//	public static void main(String[] args) {		
//	    try {
//	        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
//	 
//	        // Build a filter for the SmsList
//	        Map<String, String> params = new HashMap<String, String>();
//	        params.put("Body", "Hello World");
//	        params.put("To", "+639178994386");
//	        params.put("From", "+12242201502");
//	 
//	        SmsFactory messageFactory = client.getAccount().getSmsFactory();
//	        Sms message2 = messageFactory.create(params);
//	        System.out.println(message2.getSid());
//	    } catch (TwilioRestException e) {
//	        System.out.println(e.getErrorMessage());
//	    }
//	}
}
